﻿#region Copyright (c) 2007-2010 Mikko Salmi, Contributors
/*
 *
 * Copyright (c) 2007-2010 Mikko Salmi, Contributors
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
*/
#endregion // Copyright

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebPartPages;

using System.Text;

namespace CodePlex.WebParts
{
    public static class WebPartTools
    {
        public static void CreateStyleSheetLink(Page page, string styleSheetUrl)
        {
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = styleSheetUrl;
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");

            if (page.Header != null)
                page.Header.Controls.Add(cssLink);
        }

        public static void CreateScriptLink(Page page, string scriptUrl)
        {
            LiteralControl lc = new LiteralControl();
            lc.Text = "<script type=\"text/javascript\" src=\"" + scriptUrl + "\"></script>";

            if (page.Header != null)
                page.Header.Controls.Add(lc);
        }

        /// <summary>
        /// Return all toolparts from EditorPartCollection and add EditorPart as last.
        /// </summary>
        /// <returns>EditorPartCollection</returns>
        public static EditorPartCollection AddEditorPart(EditorPartCollection editorPartCollection, EditorPart addEditorPart)
        {
            EditorPartCollection resultEditorPartCollection = new EditorPartCollection();

            try
            {
                EditorPart[] editorPartArray = new EditorPart[editorPartCollection.Count + 1];
                editorPartCollection.CopyTo(editorPartArray, 0);
                editorPartArray[editorPartArray.Length - 1] = addEditorPart;
                resultEditorPartCollection = new EditorPartCollection(editorPartArray);
            }
            catch
            {
            }

            return resultEditorPartCollection;
        }

        public static void addDelimitedStringBuilderString(ref StringBuilder stringBuidlder, string value, string delimiter)
        {
            //add delimiter to non empty string
            if (stringBuidlder.Length > 0)
            {
                stringBuidlder.Append(delimiter);
            }

            //add value itself
            stringBuidlder.Append(value);
        }
    }
}
